package zhuyun.code.launcheractivity.utils

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import java.util.*

/**
 * @author: Administrator
 * @date: 2021/11/17
 */
object CodeUtils {
    private val CHARS = listOf<Char>('0','1','2','3','4','5','6','7','8','9')
    private val mBuilder = StringBuilder()
    private val mRandom = Random()
    private const val DEFAULT_CODE_LENGTH = 4 //验证码的长度  这里是6位
    private const val DEFAULT_FONT_SIZE = 60.0f //字体大小
    private const val DEFAULT_LINE_NUMBER = 3 //多少条干扰线
    private const val BASE_PADDING_LEFT = 40 //左边距
    private const val RANGE_PADDING_LEFT = 30 //左边距范围值
    private const val BASE_PADDING_TOP = 70 //上边距
    private const val RANGE_PADDING_TOP = 15 //上边距范围值
    private const val DEFAULT_WIDTH = 400 //默认宽度.图片的总宽
    private const val DEFAULT_HEIGHT = 100 //默认高度.图片的总高
    private const val DEFAULT_COLOR = 0xDF //默认背景颜色值
    private var mPaddingLeft:Int = 0
    private var mPaddingTop:Int = 0
    lateinit var code:String
    fun createBitmap():Bitmap{
        mPaddingLeft = 0
        mPaddingTop = 0
        val bitmap = Bitmap.createBitmap(DEFAULT_WIDTH, DEFAULT_HEIGHT, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        code = createCode()
        canvas.drawColor(Color.rgb(DEFAULT_COLOR,DEFAULT_COLOR,DEFAULT_COLOR))
        val paint = Paint()
        paint.textSize = DEFAULT_FONT_SIZE
        for(ch in code){
            randomTextStyle(paint)
            randomPadding()
            canvas.drawText("$ch", mPaddingLeft.toFloat(), mPaddingTop.toFloat(),paint)

        }
        for(i in 0 until DEFAULT_LINE_NUMBER){
            drawLine(canvas,paint)
        }
        canvas.save()
        canvas.restore()
        return bitmap


    }
    private fun drawLine(canvas: Canvas,paint: Paint){
        val color = randomColor()
        val startX = mRandom.nextInt(DEFAULT_WIDTH)
        val startY = mRandom.nextInt(DEFAULT_HEIGHT)
        val stopX = mRandom.nextInt(DEFAULT_WIDTH)
        val stopY = mRandom.nextInt(DEFAULT_HEIGHT)
        paint.strokeWidth = 1.0f
        paint.color = color
        canvas.drawLine(startX.toFloat(),startY.toFloat(),stopX.toFloat(),stopY.toFloat(),paint)

    }
    private fun randomPadding(){
        mPaddingLeft += BASE_PADDING_LEFT + mRandom.nextInt(RANGE_PADDING_LEFT)
        mPaddingTop = BASE_PADDING_TOP + mRandom.nextInt(RANGE_PADDING_TOP)
    }
    private fun randomTextStyle(paint: Paint){
        val color = randomColor()
        paint.color = color
        paint.isFakeBoldText = mRandom.nextBoolean()
        var skewX = mRandom.nextInt(11)/10.0f
        skewX = if(mRandom.nextBoolean()) skewX else -skewX
        paint.textSkewX = skewX

    }
    private fun randomColor():Int{
        mBuilder.delete(0, mBuilder.length)
        for(i in 0 until 3){
            var hexString = Integer.toHexString(mRandom.nextInt(0xFF))
            if(hexString.length == 1) {
                hexString = "0$hexString"
            }
            mBuilder.append(hexString)
        }
        return Color.parseColor("#${mBuilder.toString()}")
    }
    private fun createCode():String{
        mBuilder.delete(0, mBuilder.length)
        for(i in 0 until DEFAULT_CODE_LENGTH){
            mBuilder.append(CHARS[mRandom.nextInt(CHARS.size)])
        }
        return mBuilder.toString()
    }


}